package com.cn.kabalifeuser.ui.service;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.bean.AddressProvinceListBean;
import com.cn.kabalifeuser.ui.service.bean.BannerListBean;
import com.cn.kabalifeuser.ui.service.bean.BusinessListBean;
import com.cn.kabalifeuser.ui.service.bean.CityIdBean;
import com.cn.kabalifeuser.ui.service.bean.IndexTypeListBean;
import com.cn.kabalifeuser.ui.service.bean.NewListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.Map;

/**
 * Created by 晓林 on 2020/9/2.
 * function:
 */
public class FragmentServicePresenter extends BasePresenter<FragmentServiceView> {
    //top 轮播
    public void onBannerData(String token, int type) {
        OkGo.<BannerListBean>get(Api.API_INDEX_BANNER)
                .headers("token", token)
                .params("city_id", LoginHelper.getCityData().getResult().getPid())
                .params("type", type)
                .execute(new CacheCallBack<BannerListBean>() {
                    @Override
                    public void onSuccess(Response<BannerListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("banner：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onBannerSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    public void onBannerTjData(String token, int type) {
        OkGo.<BannerListBean>get(Api.API_INDEX_BANNER)
                .headers("token", token)
                .params("type", type)
                .params("city_id", LoginHelper.getCityData().getResult().getPid())
                .execute(new CacheCallBack<BannerListBean>() {
                    @Override
                    public void onSuccess(Response<BannerListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("banner：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onBannerTjSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 广播
     *
     * @param token
     * @param page
     * @param limit
     */
    public void onNewListData(String token, int page, int limit) {
        OkGo.<NewListBean>post(Api.API_INDEX_NEW_LIST)
                .headers("token", token)
                .params("page", page)
                .params("limit", limit)
                .execute(new CacheCallBack<NewListBean>() {
                    @Override
                    public void onSuccess(Response<NewListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("广播：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onNewListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 获取当前区
     *
     * @param name
     * @param city_name
     */
    public void onCityIdData(String name, String city_name) {
        LoggerUtils.e("市区：" + name + city_name);
        OkGo.<CityIdBean>get(Api.API_INDEX_CITY_ID)
                .params("name", name)
                .params("city_name", city_name)
                .execute(new CacheCallBack<CityIdBean>() {
                    @Override
                    public void onSuccess(Response<CityIdBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("获取当前区：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCityIdSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 商户列表
     *
     * @param token
     * @param map
     */
    public void onBusinessListData(String token, Map<String, Object> map) {
        OkGo.<BusinessListBean>get(Api.API_INDEX_BUSINESS_LIST)
                .headers("token", token)
                .params("is_reco", map.get("is_reco").toString())
                .params("lon", map.get("lon").toString())
                .params("lat", map.get("lat").toString())
                .params("juli", map.get("juli").toString())
                .params("fraction", map.get("fraction").toString())
                .params("area", map.get("area").toString())
                .params("city", map.get("city").toString())
                .params("page", map.get("page").toString())
                .params("limit", map.get("limit").toString())
                .execute(new CacheCallBack<BusinessListBean>() {
                    @Override
                    public void onSuccess(Response<BusinessListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("商户列表：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onBusinessListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }


    //推薦
    public void onRecommendListData(String token, Map<String, Object> map) {
        OkGo.<BusinessListBean>get(Api.API_INDEX_BUSINESS_LIST)
                .headers("token", token)
                .params("is_reco", map.get("is_reco").toString())
                .params("lon", map.get("lon").toString())
                .params("lat", map.get("lat").toString())
                .params("want", map.get("want").toString())
                .params("juli", map.get("juli").toString())
                .params("fraction", map.get("fraction").toString())
                .params("area", map.get("area").toString())
                .params("city", map.get("city").toString())
                .params("page", map.get("page").toString())
                .params("limit", map.get("limit").toString())
                .execute(new CacheCallBack<BusinessListBean>() {
                    @Override
                    public void onSuccess(Response<BusinessListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("推荐：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onRecommendListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }


    /**
     * 分类
     *
     * @param token
     * @param limit
     * @param type
     */
    public void onTypeData(String token, int limit, int type) {
        OkGo.<IndexTypeListBean>get(Api.API_INDEX_TYPE_LIST)
                .headers("token", token)
                .params("limit", limit)
                .params("type", type)
                .execute(new CacheCallBack<IndexTypeListBean>() {
                    @Override
                    public void onSuccess(Response<IndexTypeListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("分类：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onTypeSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
    /**
     * 分类
     *
     * @param token
     * @param type
     */
    public void onTypeTData(String token, int type) {
        OkGo.<IndexTypeListBean>get(Api.API_INDEX_TYPE_LIST)
                .headers("token", token)
                .params("type", type)
                .execute(new CacheCallBack<IndexTypeListBean>() {
                    @Override
                    public void onSuccess(Response<IndexTypeListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("分类：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onTypeTSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
    /**
     * 你希望找什么服务提交
     *
     * @param token
     * @param want_type
     */
    public void onWantPushData(String token, String want_type) {
        OkGo.<MsgBean>post(Api.API_INDEX_WANPUSH)
                .headers("token", token)
                .params("want_type", want_type)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("banner：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onWantPushSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    //市
    public void onCityListData(int pid) {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", pid)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("市：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCityListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    //省
    public void onProvinceListData() {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", 1)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("省：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onProvinceListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }
}
